#include <cstring>
#include <iostream>

void get_next(const char *str, int len, int next[])
{
    int i = 1, j = 0;
    next[1] = 0;
    while (i < len) {
        if (j == 0 || str[i] == str[j]) {
            ++i;
            ++j;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
}

void get_next_adv(const char *str, int len, int next[])
{
    int i = 1, j = 0;
    next[1] = 0;
    while (i < len) {
        if (j == 0 || str[i] == str[j]) {
            ++i;
            ++j;
            next[i] = j;
        } else {
            if (str[next[j]] == str[j]) {
                j = next[next[j]];
            } else {
                j = next[j];
            }
        }
    }
}

int kmp(const char *str_s, const char *str_t, int len_s, int len_t)
{
    int next[20] = {};
    get_next_adv(str_t, len_t, next);
    int i = 1, j = 1;
    while (i <= len_s && j <= len_t) {
        if (str_s[i - 1] == str_t[j - 1] || j == 0) {
            ++i;
            ++j;
        } else {
            j = next[j];
        }
    }
    if (j > len_t) {
        return i - len_t;
    }

    return 0;
}

int main()
{
    const char *tmp = "abcabcmb";
    const char *str = "abcabdddabcabcmbbb";
    std::cout << strlen(str) << std::endl;
    std::cout << strlen(tmp) << std::endl;
    std::cout << kmp(str, tmp, strlen(str), strlen(tmp)) << std::endl;

    return 0;
}